﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DM_TOOLS.Tools.DBC.HEX
{
    public class FrameDataConverter : CsvHelper.TypeConversion.DefaultTypeConverter
    {
        public override object ConvertFromString(string? text, CsvHelper.IReaderRow row, CsvHelper.Configuration.MemberMapData memberMapData)
        {
            if (string.IsNullOrWhiteSpace(text))
                return Array.Empty<byte>();

            return text.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                       .Select(b => Convert.ToByte(b, 16))
                       .ToArray();
        }

        public override string ConvertToString(object? value, CsvHelper.IWriterRow row, CsvHelper.Configuration.MemberMapData memberMapData)
        {
            if (value is byte[] bytes)
                return string.Join(" ", bytes.Select(b => b.ToString("X2")));
            return base.ConvertToString(value, row, memberMapData);
        }
    }
}
